import Vue from 'vue';
import Router from 'vue-router';
import hooks from './hook'; //获取路由全部的钩子import hooks from './hook'; //获取路由全部的钩子

Vue.use(Router)

let routes = []
const routerContext = require.context('./', true, /index\.js$/);
routerContext.keys().forEach(route => {
	// 如果是根目录的index.js不处理
	if (route.startsWith('./main')) {
		return
	}
	const routerMoudule = routerContext(route)
	routes = [...routes, ...(routerMoudule.default || routerMoudule)]
})

routes.forEach(route => {
	route.path = route.path || '/' + (route.name || '');
});

console.log(process.env.BASE_URL);

const router = new Router({
	mode: 'hash',
	base: process.env.BASE_URL || '/',
	routes,
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition
		} else {
			return {
				x: 0,
				y: 0
			}
		}
	}
});

// Object.values(hooks).forEach(hook => {
// 	/// 路由切换之前
// 	router.beforeEach(hook)
// });

export default router
